package cn.hbads.support.json.support;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

/**
 * <b>输入流的反序列化器，将Base64编码的byte[]数组转换回InputStream</b>
 * <br><br/>可以通过 @JsonSerialize(using = InputStreamDeserializer.class)使用
 */
public class InputStreamDeserializer extends JsonDeserializer<InputStream> {
    @Override
    public InputStream deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException {
        // 从 Base64 字符串重建 InputStream
        String base64 = p.getText();
        byte[] bytes = Base64.getDecoder().decode(base64);
        return new ByteArrayInputStream(bytes);
    }
}